package com.health.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.system.entity.HealthLog;
import com.health.system.mapper.LogMapper;
import com.health.system.service.LogService;
import com.health.system.vo.LogListPageSearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, HealthLog> implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public IPage<HealthLog> selectInfoListPage(Long current, Long size, LogListPageSearchVo condition) {
        IPage<HealthLog> page = new Page<>(current, size);
        return logMapper.selectPage(page, new LambdaQueryWrapper<HealthLog>()
                .eq(HealthLog::getType, 0)
                .eq(StringUtils.isNotBlank(condition.getKeyword()), HealthLog::getOptModule, condition.getKeyword())
                .or()
                .eq(StringUtils.isNotBlank(condition.getKeyword()), HealthLog::getOptDesc, condition.getKeyword())
        );
    }

    @Override
    public IPage<HealthLog> selectErrorListPage(Long current, Long size, LogListPageSearchVo condition) {
        IPage<HealthLog> page = new Page<>(current, size);
        return logMapper.selectPage(page, new LambdaQueryWrapper<HealthLog>()
                .eq(HealthLog::getType, 1)
                .eq(StringUtils.isNotBlank(condition.getKeyword()), HealthLog::getOptModule, condition.getKeyword())
                .or()
                .eq(StringUtils.isNotBlank(condition.getKeyword()), HealthLog::getOptDesc, condition.getKeyword())
        );
    }

    @Async(value = "HealthLogExecutor")
    @Override
    public void asyncInsert(HealthLog log) {
        logMapper.insert(log);
    }
}
